package org.example;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class RichFunctionDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

                DataGeneratorSource<Long> dataGeneratorSource =
                new DataGeneratorSource<>(
                        new GeneratorFunction<Long, Long>() {
                            @Override
                            public Long map(Long value) throws Exception {
                                return value;
                            }
                        },
                        Long.MAX_VALUE,
                        RateLimiterStrategy.perSecond(1),
                        Types.LONG
                );




        env.fromSource(dataGeneratorSource, WatermarkStrategy.noWatermarks(),"genSource")
                .map(new RichMapFunction<Long, Long>() {
            @Override
            public Long map(Long value) throws Exception {
                System.out.println(getRuntimeContext().getJobId());
                System.out.println(getRuntimeContext().getExecutionConfig());
                return value * 2;
            }

            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                System.out.println("open");
            }

            @Override
            public void close() throws Exception {
                super.close();
                System.out.println("close");
            }
        }).print();
        env.execute();
    }
}
